{extend name="base" /}
{block name="css"}
{/block}
{block name="body"}
<div class="layui-card">
<script type="text/html" id="toolbarDemo">
 <div class="layui-card-header">
        <div class="layui-btn-group">
            <a href="{:url('lnadmin/add')}" class="layui-btn layui-btn-sm ajax-iframe" data-width="410px" data-height="310px"><i class="fa fa-plus-circle"></i> 添加管理员</a>
        </div>
    </div> 
</script>
        <table id="tableTrack" lay-filter="tableTrack"></table>
      <!-- 表格操作列 -->
<script type="text/html" id="tableTBTrack">
            <a href="{:url('lnadmin/edit')}?id={{d.id}}" class="layui-btn layui-btn-sm ajax-iframe" data-width="410px" data-height="310px"><i class="layui-icon layui-icon-edit"></i>修改</a>
    {{#  if(d.id == '1'){ }}
   <a class="layui-btn layui-btn-disabled layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>{:__('delete')}</a>
    {{#  } else { }}
    <a href="{:url('lnadmin/del')}?id={{d.id}}&uid={$admin->id}" class="layui-btn layui-btn-danger layui-btn-sm ajax-delete" tiptext="确认删除管理员{{d.username}}"><i class="layui-icon layui-icon-delete"></i>删除</a>
    {{#  } }}
</script>
<script type="text/html" id="img">
    <div><img src="{{ d.image }}" class="layui-upload-img layui-circle" style="width: 30px; height: 30px;"></div>
</script>
<script type="text/html" id="switchTpl">
        <input type="checkbox" name="status" lay-skin="switch" lay-filter="*" lay-text="{:__('normal')}|{:__('locking')}" data-url="{:url('lnadmin/edit')}?id={{d.id}}" {{d.status==1?'checked':''}}>
    </script>
</div>
{/block}
{block name="js"}
<script>
    layui.use(['layer', 'form', 'table', 'upload', 'carousel'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var upload = layui.upload;
     //监听单元格编辑
          table.on('edit(tableTrack)', function(obj){
             var value = obj.value //得到修改后的值
                 ,data = obj.data //得到所在行所有键值
                 ,field = obj.field; //得到字段   
                  //console.log(obj.data.create_time);
                  var date = new Date(obj.data.create_time); 
                  var create_time = Date.parse(date);  
                   obj.data.create_time = create_time/1000;
                  delete obj.data.image; 
                  delete obj.data.password;   
                   obj.data.group = <?php echo htmlentities((isset($admin['group_id']) && ($admin['group_id'] !== '')?$admin['group_id']:'')); ?>;
                  var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;  
                  var strTemp = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
                  if (strTemp.test(value) && value.length == 11 || reg.test(value)) {
                  var load = layer.load(1, {time: 3*1000}); //加载动画 //加载层
            $.ajax({
                url: "{:url('lnadmin/edit')}?id=" +obj.data.id,
                dataType:'json',
                type: 'POST',
                timeout:5000,
                data:data,
                success:function(res){
                    if(res.code == 0){
                    layer.msg('操作成功');
                     setTimeout(function () {
                    //parent.location.reload();
                }, 1000);
                    }else{
                    layer.msg(res.msg, {icon: 0});
                    }
                },
                complete:function(XMLHttpRequest,status){
                    if(status!='success'){
                        layer.msg("请求出错，请稍候再试");
                    }
                }.bind(this)
            });
    
    // 其他更新操作
    var update = {};
    update[field] = value;
    obj.update(update);
        }else{
            
        return layer.msg('输入格式不正确！');
         }
     });
        // 渲染回访表格
        var insTb = table.render({
            elem: '#tableTrack',
            toolbar: '#toolbarDemo',  //开启头部工具栏，并为其绑定左侧模板
            
            url: "{:url('lnadmin/index_json')}",  // 请求数据路由地址
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                 //,curr: 5 //设定初始在第 5 页
            ,groups: 1 //只显示 1 个连续页码
            ,first: false //不显示首页
            ,last: false //不显示尾页
            ,prev: '<em>上一页</em>'
            ,next: '<em>下一页</em>'
            },
            lineStyle: 'height: 100px;',//头像高度
            cellMinWidth: 100,
            size:'lg',even:true,
            cols: [[
                  {
                    type:'numbers',
                    title:'#'
                }
                , {
                    field: 'image', 
                    align: 'center', 
                    templet: function (d) {
                        var url = d.image;
                        return '<img src="' + url + '" class="tdImg" tb-img/>';
                     }, 
                     title: '图片', 
                     width: 140, 
                     unresize: true
                   }
                , {
                    field: 'group_name', 
                    title: '权限组', 
                    width: 120, 
                    align: 'center', 
                    templet: function(d){
                        var load = d.group_id==1 ? '-red' : (d.group_id==2 ? '-green' : (d.group_id==3 ? '-orange' : (d.group_id==4 ? '-blue' : '')));
                    return '<span class="layui-btn layui-btn-primary layui-btn-xs layui-border' + load + '">' + d.group_name + '</span>';
                    }
                }
                , {
                    field: 'username', 
                    title: '管理员名称', 
                    align: 'center', 
                    width: 120, 
                    style:'background-color: #eee; color: #ff1500;',

                }
                , {
                    field: 'iphone', 
                    title: '苹果手机', 
                    align: 'center', 
                    width: 120,
                    fieldTitle: '苹果手机',
                    expandedMode: 'tips',
                    edit: 'text'
                }
                , {field:'email', title:'邮箱 <i class="layui-icon layui-icon-tips layui-font-14" lay-event="email-tips" title="该字段开启了编辑功能" style="margin-left: 5px;"></i>', fieldTitle: '邮箱', hide: 0, width:150, expandedMode: 'tips', edit: 'text'}
                , {
                    field: 'login_time', 
                    title: '登录时间', 
                    align: 'center', 
                    width: 160, 
                    templet:"<div>{{layui.util.toDateString(d.login_time*1000, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {
                    field: 'create_time', 
                    title: '创建时间', 
                    align: 'center', 
                    width: 160
                }
                , {
                    field:'status', 
                    title:'是否锁定', 
                    width:110, 
                    templet: '#switchTpl', 
                    unresize: true
                }
                , {
                    align: 'center', 
                    toolbar: '#tableTBTrack', 
                    title: '操作', 
                    minWidth: 200
                }
               ]],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                  "code": res.code, //解析接口状态
                  "msg": res.msg, //解析提示文本
                  "count": res.data.total, //解析数据长度
                  "data": res.data.data //解析数据列表
                };
            }
        });
  // 表头自定义元素工具事件 --- 2.8.8+
  table.on('colTool(tableTrack)', function(obj){
    var event = obj.event;
    console.log(obj);
    if(event === 'email-tips'){
      layer.alert(layui.util.escape(JSON.stringify(obj.col)), {
        title: '当前列属性配置项'
      });
    }
  });
    
    });
</script>
{/block}